$(function () {
  // 按钮是否能点击
  var loginBtnBln = false;
  var registerBtnBln = false;
  // 提示处理     options:{value:提示语   type:类型类名  "error"  "normal"  "success"  callback:回调函数}
  function handleTips(options) {
    // 定时器
    var timer;
    // 提示语盒子
    var msgBox = $(".message");
    // 提示语元素
    var msgEle = $(".message span");
    // id名
    var $id = options.type || "normal";
    msgBox.attr("id", $id);
    msgEle.html(options.value);
    msgBox.show();
    timer = setTimeout(function () {
      msgBox.hide();
      clearTimeout(timer);
    }, 1500);
    // 回调
    if (options.callback) {
      options.callback();
    }
  }
  // 清空表单
  function clearInput() {
    $("#formBox input").val("");
  }
  // 登录注册切换
  $(".toLogin").on("click", function () {
    clearInput();
    $(".registerBox").hide();
    $(".loginBox").show();
  });
  $(".toRegister").on("click", function () {
    clearInput();
    $(".loginBox").hide();
    $(".registerBox").show();
  });
  // 登录按钮
  $(".loginBtn").on("click", function () {
    if (loginBtnBln) return;
    loginBtnBln = true;
    // 1500ms后解开按钮
    var timer = setTimeout(function () {
      loginBtnBln = false;
      clearTimeout(timer);
    }, 1500);
    var $name = $("#login_name").val();
    var $password = $("#login_password").val();
    // 非空验证
    if ($name === "") {
      handleTips({
        value: "账号不能为空",
        callback: function () {
          $("#login_name").focus();
        },
      });
      return;
    }
    if ($password === "") {
      handleTips({
        value: "密码不能为空",
        callback: function () {
          $("#login_password").focus();
        },
      });
      return;
    }
    // 登录
    $.ajax({
      type: "POST",
      url: "http://8.129.144.128:7000/user/login",
      data: {
        account: $name,
        password: $password,
      },
      success: function (res) {
        if (res.code === 1) {
          handleTips({
            value: res.message,
            type: "success",
            callback: function () {
              $.cookie("info", JSON.stringify(res.info), {
                expires: 7,
                path: "/",
              });
              clearInput();
              window.location.href = "websocket.html";
            },
          });
        } else {
          handleTips({
            value: res.message,
            type: "error",
            callback: function () {
              $("#login_name").focus();
            },
          });
        }
      },
    });
  });
  // 注册按钮
  $(".registerBtn").on("click", function () {
    if (registerBtnBln) return;
    registerBtnBln = true;
    // 1500ms后解开按钮
    var timer = setTimeout(function () {
      registerBtnBln = false;
      clearTimeout(timer);
    }, 1500);
    var $name = $("#register_name").val();
    var $password = $("#register_password").val();
    var $compassword = $("#register_compassword").val();
    // 非空验证
    if ($name === "") {
      handleTips({
        value: "账号不能为空",
        callback: function () {
          $("#register_name").focus();
        },
      });
      return;
    }
    if ($password === "") {
      handleTips({
        value: "密码不能为空",
        callback: function () {
          $("#register_password").focus();
        },
      });
      return;
    }
    // 确认密码验证
    if ($compassword === "") {
      handleTips({
        value: "确认密码不能为空",
        callback: function () {
          $("#register_compassword").focus();
        },
      });
      return;
    }
    // 确认密码验证
    if ($compassword !== $password) {
      handleTips({
        value: "密码不一致",
        callback: function () {
          $("#register_compassword").focus();
        },
      });
      return;
    }
    // 注册
    $.ajax({
      type: "POST",
      url: "http://8.129.144.128:7000/user/register",
      data: {
        account: $name,
        password: $password,
      },
      success: function (res) {
        if (res.code === 1) {
          handleTips({
            value: res.message,
            type: "success",
            callback: function () {
              clearInput();
              $(".registerBox").hide();
              $(".loginBox").show();
            },
          });
        } else {
          handleTips({
            value: res.message,
            type: "error",
            callback: function () {
              $("#register_name").focus();
            },
          });
        }
      },
    });
  });

  //  关闭提示按钮
  $(".close").on("click", function () {
    $(".message").hide();
  });

  // 按下确认键
  $(window).on("keydown", function (event) {
    var e = event || window.event;
    if (e.keyCode !== 13) return;
    // 登录
    var loginBln = $(".loginBox").css("display") === "none";
    // 注册
    var registerBln = $(".registerBox").css("display") === "none";
    if (!loginBln) {
      // 登录input
      var $name = $("#login_name").val();
      var $password = $("#login_password").val();
      if ($name !== "" || $password !== "") {
        $(".loginBtn").click();
      }
    }
    if (!registerBln) {
      // 注册input
      var $name = $("#register_name").val();
      var $password = $("#register_password").val();
      var $compassword = $("#register_compassword").val();
      if ($name !== "" || $password !== "" || $compassword !== "") {
        $(".registerBtn").click();
      }
    }
  });
});
